{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 图像模糊（图像平滑)\n",
    "### 1. 均值滤波\n",
    "这是由一个归一化卷积框完成的。它取卷积核区域下所有像素的平均值并替换中心元素。这是由函数cv.blur()或cv.boxFilter()完成的。查看文档以获取有关卷积核的更多详细信息。我们应该指定卷积核的宽度和高度，3x3标准化的盒式过滤器如下所示：\n",
    "\n",
    "$$ K=\\frac{1}{9}\\begin{bmatrix} \\ 1 \\ \\ 1\\ \\ 1\\ \\ 1 \\ \\ 1\\ \\ 1\\ \\ 1 \\ \\ 1\\ \\ 1 \\end{bmatrix} $$\n",
    "\n",
    "注意：如果不想使用规范化的框过滤器，请使用cv.boxFilter()。将参数normalize = False传递给函数。\n",
    "\n",
    "使用5x5大小的卷积核检查下面的示例演示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% code\n"
    }
   },
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "img = cv.imread(\"img/mh/yb_zhandou.jpg\", 1)\n",
    "img = cv.resize(img, (300, 300))\n",
    "\n",
    "blur = cv.blur(img,(5,5))\n",
    "\n",
    "plt.subplot(121), plt.imshow(img), plt.title(\"img\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.subplot(122), plt.imshow(blur), plt.title(\"blur\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 2. 高斯滤波\n",
    "下面把卷积核换成高斯核。它是通过函数cv.GaussianBlur()完成的。我们应该指定卷积核的宽度和高度，它应该是正数并且是奇数。我们还应该分别指定X和Y方向的标准偏差sigmaX和sigmaY。如果仅指定了sigmaX，则sigmaY与sigmaX相同。如果两者都为零，则根据卷积核大小计算它们。高斯模糊在从图像中去除高斯噪声方面非常有效。\n",
    "\n",
    "如果需要，可以使用函数cv.getGaussianKernel()创建高斯卷积核。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "img = cv.imread(\"img/cat.jpg\", 1)\n",
    "img = cv.resize(img, (300, 300))\n",
    "\n",
    "blur = cv.GaussianBlur(img, (5, 5), 0)\n",
    "\n",
    "plt.subplot(121), plt.imshow(img), plt.title(\"img\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.subplot(122), plt.imshow(blur), plt.title(\"blur\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 中值滤波\n",
    "顾名思义，函数cv.medianBlur()取卷积核区域下所有像素的中值，并用该中值替换中心元素。这对去除图像中的椒盐噪声非常有效。有趣的是，在上述滤波器中，中心元素是新计算的值，其可以是图像中的像素值或新值。但在中值模糊中，中心元素总是被图像中的某个像素值替换,它有效地降低了噪音。其卷积核大小应为正整数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "img = cv.imread(\"img/cat.jpg\", 1)\n",
    "img = cv.resize(img, (300, 300))\n",
    "\n",
    "median = cv.medianBlur(img,5)\n",
    "\n",
    "plt.subplot(121), plt.imshow(img), plt.title(\"img\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.subplot(122), plt.imshow(median), plt.title(\"median\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 双边过滤\n",
    "cv.bilateralFilter()在降低噪音方面非常有效，同时保持边缘清晰。但与其他过滤器相比，操作速度较慢。\n",
    "我们已经看到高斯滤波器采用像素周围的邻域并找到其高斯加权平均值。该高斯滤波器仅是空间的函数，即在滤波时考虑附近的像素。它没有考虑像素是否具有几乎相同的强度。它不考虑像素是否是边缘像素。所以它也模糊了边缘，我们不想这样做。\n",
    "\n",
    "双边滤波器在空间中也采用高斯滤波器，但是还有一个高斯滤波器是像素差的函数。空间的高斯函数确保仅考虑附近的像素用于模糊，而强度差的高斯函数确保仅考虑具有与中心像素相似的强度的像素用于模糊。因此它保留了边缘，因为边缘处的像素将具有较大的强度变化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "img = cv.imread(\"img/cat.jpg\", 1)\n",
    "img = cv.resize(img, (300, 300))\n",
    "\n",
    "blur = cv.bilateralFilter(img,9,75,75)\n",
    "\n",
    "plt.subplot(121), plt.imshow(img), plt.title(\"img\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.subplot(122), plt.imshow(blur), plt.title(\"blur\")\n",
    "plt.xticks([]), plt.yticks([])\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
